/**
 *@Description  全局配置，及其公用方法。
 *@Author mengjun
 *@date 2019/10/915:18
 *@Version 1.0
 */



// 公用常量
var BASE = {
    DOMAIN: "http://192.168.7.7:8080",
    API: "http://192.168.7.7:8082/api/v1",
    FILE_PATH: "",
}


// 通用工具
var TOOLS = {
    // msg 弹框停留时间
    MSG_TIME: 2500,
    // 获取参数
    getUrlParam: function (key) {
        var url = window.location.search;
        // 正则筛选地址栏
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        // 匹配目标参数
        var result = url.substr(1).match(reg);
        //返回参数值
        return result ? decodeURIComponent(result[2]) : null;
    },
    SUCCESS: function (msg) {
        layui.layer.msg(TOOLS.EMPTY_TO_STRING(msg), {icon: 1, time: TOOLS.MSG_TIME})
        return false;
    },
    //带钩子函数的成功提示
    SUCCESS_Hook: function (msg, handle) {
        layui.layer.msg(TOOLS.EMPTY_TO_STRING(msg), {
            icon: 1, time: 2000, end: function () {
                handle();
            }
        })
        return false;
    },
    WARN: function (msg) {
        layui.layer.msg(TOOLS.EMPTY_TO_STRING(msg), {icon: 2, time: TOOLS.MSG_TIME});
        return false;
    },
    ERROR: function () {
        layui.layer.msg("服务器繁忙，请稍后重试！", {icon: 5, time: TOOLS.MSG_TIME});
        return false;
    },
    GET_SIMPLE_TEXT: function (html) {
        var re1 = new RegExp("<.+?>", "g");//匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
        var msg = html.replace(re1, '');//执行替换成空字符
        return msg;
    },
    CONVERT_URL: function (url) {
        if (url.indexOf("http://") != -1) {
            return "http://" + url;
        } else {
            return url;
        }
    },
    CREATE_PAGINATION: function (CURRENT_PAGE, elem, count, limit, objModule, flag) {
        layui.use(['laypage'], function () {
            var laypage = layui.laypage;
            //完整功能
            laypage.render({
                curr: CURRENT_PAGE,
                elem: elem
                , count: count
                , limit: limit
                , prev: '上一页'
                , next: '下一页'
                , theme: '#be4c4d'
                , layout: ['count', 'prev', 'page', 'next']
                , jump: function (obj, first) {
                    if (!first) {
                        objModule.BaseSelect(obj.curr, flag);
                    }
                }
            });
        });
    },
    Stop_The_Event: function () {
        var e = (e) ? e : ((window.event) ? window.event : null);
        window.event ? window.event.cancelBubble = true : e.stopPropagation();
    },

    //设置cookie
    setCookie: function (cname, cvalue, exdays, path) {
        $.cookie(cname, cvalue, {"expires": exdays, "path": path});
    }
    ,
    //获取cookie
    getCookie: function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }
    ,
    //清除cookie See https://www.cnblogs.com/wangmaoling/p/7745005.html
    clearCookie: function (name, path) {
        $.cookie(name, "", {"expires": -1, "path": path});
    }
    ,
    clearCookie: function (name) {
        $.cookie(name, "", {"expires": -1, "path": "/"});
    }
    ,
    isEmpty: function (v) {
        if (v == undefined || v == "" || v.length == 0 || v == null || v == "null") {
            return true;
        }
        return false;
    },
    EMPTY_TO_STRING: function (v) {
        if (v == undefined || v == "" || v.length == 0) {
            return "未知";
        }
        return v;
    }
    ,
    //去除富文本标签
    rich: function (content) {
        content = content.replace(/(\n)/g, "");
        content = content.replace(/(\t)/g, "");
        content = content.replace(/(\r)/g, "");
        content = content.replace(/<\/?[^>]*>/g, "");
        content = content.replace(/\s*/g, "");
        content = content.replace(/&nbsp;/g, "");
        return content;
    }
    ,
    //监测图片error
    imgError: function (image) {
        $(image).attr("src", "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2306401220,2831614423&fm=27&gp=0.jpg");
        $(image).attr("onerror", ""); // 防止onerror函数无限触发
    }
    ,
    headError: function (image) {
        $(image).attr("src", "/Cloud/WebUI/img/defaultImg.png");
        $(image).attr("onerror", ""); // 防止onerror函数无限触发
    }
    ,
    // 小数转百分比
    decimal2Percentage: function (decimal) {
        var str = Number(decimal * 100).toFixed(0);
        str += "%";
        return str;
    }
    ,
    base64ToBlob: function (urlData, type) {
        let arr = urlData.split(',');
        let mime = arr[0].match(/:(.*?);/)[1] || type;
        // 去掉url的头，并转化为byte
        let bytes = window.atob(arr[1]);
        // 处理异常,将ascii码小于0的转换为大于0
        let ab = new ArrayBuffer(bytes.length);
        // 生成视图（直接针对内存）：8位无符号整数，长度1个字节
        let ia = new Uint8Array(ab);
        for (let i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
        }
        return new File([ab], Date.parse(new Date()) + ".jpg", {
            type: 'image/jpg'
        });
    }
}

// 公用初始化
var INIT = {
    load: function () {
        // TODO ...
        // ....
    },
}


$(function () {
    // INIT.load();
})






